package com.atguigu.gmall.user.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.MD5;
import com.atguigu.gmall.user.entity.UserInfo;
import com.atguigu.gmall.user.service.UserInfoService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/user")
@CrossOrigin
public class UserController {

    @Autowired
    UserInfoService userInfoService;


    @GetMapping("/list/{page}/{size}")
    public Result userList(@PathVariable Integer page,@PathVariable Integer size) {
        Page<UserInfo> userInfoPage = new Page<>(page,size);
        Page<UserInfo> pageList = userInfoService.page(userInfoPage);
        return Result.ok(pageList);
    }

    @DeleteMapping("/delete/{id}")
    public Result userList(@PathVariable Long id) {
        userInfoService.removeById(id);
        return Result.ok();
    }

    @PostMapping("/save")
    public Result save(@RequestBody UserInfo userInfo) {
        if (userInfo.getId() != null) {
            userInfoService.updateById(userInfo);
        }else {
            userInfo.setPasswd(MD5.encrypt(userInfo.getPasswd()));
            userInfoService.save(userInfo);
        }
        return Result.ok();
    }
    @GetMapping("/getById/{id}")
    public Result save(@PathVariable Long id) {
        UserInfo byId = userInfoService.getById(id);
        return Result.ok(byId);
    }



}
